#include<bits/stdc++.h>
using namespace std;
class Trie
{
public:
    unordered_map<char,Trie*> children;
    int count=0;

    Trie* insert(char ch)
    {
        if(!this->children.count(ch))
        {
            this->children[ch]=new Trie();
            count++;
        }
        return this->children[ch];
    }
};

class Solution {
public:
    int minimumLengthEncoding(vector<string>& words) {
        for(int i=0;i<words.size();i++)
            reverse(words[i].begin(),words[i].end());
        unordered_map<Trie*,int> hash;
        Trie* root=new Trie();
        int ans=0;
        for(int i=0;i<words.size();++i)
        {
            string s=words[i];
            Trie* cur=root;
            for(char ch:s)
            {
                cur=cur->insert(ch);
            }
            hash[cur]=i;
        }
        for(auto&[cur,index]:hash)
        {
            if(cur->count==0) ans+=words[index].size()+1;
        }
        return ans;
    }
};
